package com.my.dao.impl;

import java.math.BigInteger;
import java.util.List;

import com.my.bean.Order;
import com.my.dao.IBaseDAO;
import com.my.dao.OrderDAO;

public class OrderDAOImpl implements OrderDAO {
	private IBaseDAO<Order> basedao;

	public void setBasedao(IBaseDAO<Order> basedao) {
		this.basedao = basedao;
	}

	@Override
	public boolean addOrder(Order order) throws Exception {
		if (this.checkOrder(order)) {
			return basedao.add(order);
		}
		return false;
	}

	@Override
	public boolean editOrder(Order order) throws Exception {
		if (this.checkOrder(order)) {
			return basedao.edit(order);
		}
		return false;
	}

	@Override
	public Order getOrderById(Class<Order> c, Integer id) throws Exception {
		return basedao.getById(c, id);
	}

	@Override
	public boolean removeOrder(Integer id) throws Exception {
		Order order = basedao.getById(Order.class, id);
		if (order != null) {
			basedao.remove(order);
		}
		return false;
	}

	@Override
	public BigInteger getTotalOrderRecords() throws Exception {
		String sql = "select count(order_id) from info_order where state='未受理'";
		return basedao.getTotalRecords(sql);
	}

	@Override
	public List<Order> getAllOrderByPage(int pageindex, int pagesize)
			throws Exception {
		String hql = "from Order as o where o.state='未受理'";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize);
	}

	@Override
	public BigInteger searchTotalOrderRecords(Order order) throws Exception {
		String sql = "select count(a.order_id) from info_order a inner join info_user b on a.user_id=b.user_id where b.province like ? and b.city like ? and a.order_no like ? and a.state='未受理'";
		String item1 = order.getUser().getProvince() + "%";
		String item2 = order.getUser().getCity() + "%";
		String item3 = order.getNo() + "%";
		return basedao.getTotalRecords(sql, item1, item2, item3);
	}

	@Override
	public List<Order> searchOrderByPage(int pageindex, int pagesize,
			Order order) throws Exception {
		String hql = "from Order as o where o.user.province like ? and o.user.city like ? and o.no like ? and o.state='未受理'";
		String item1 = order.getUser().getProvince() + "%";
		String item2 = order.getUser().getCity() + "%";
		String item3 = order.getNo() + "%";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize, item1,
				item2, item3);
	}

	/******************************************* 2 *********************************************************/

	@Override
	public BigInteger getTotalOrderRecords2() throws Exception {
		String sql = "select count(order_id) from info_order where state='已受理'";
		return basedao.getTotalRecords(sql);
	}

	@Override
	public List<Order> getAllOrderByPage2(int pageindex, int pagesize)
			throws Exception {
		String hql = "from Order as o where o.state='已受理'";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize);
	}

	@Override
	public BigInteger searchTotalOrderRecords2(Order order) throws Exception {
		String sql = "select count(a.order_id) from info_order a inner join info_user b on a.user_id=b.user_id where b.province like ? and b.city like ? and a.order_no like ? and a.state='已受理'";
		String item1 = order.getUser().getProvince() + "%";
		String item2 = order.getUser().getCity() + "%";
		String item3 = order.getNo() + "%";
		return basedao.getTotalRecords(sql, item1, item2, item3);
	}

	@Override
	public List<Order> searchOrderByPage2(int pageindex, int pagesize,
			Order order) throws Exception {
		String hql = "from Order as o where o.user.province like ? and o.user.city like ? and o.no like ? and o.state='已受理'";
		String item1 = order.getUser().getProvince() + "%";
		String item2 = order.getUser().getCity() + "%";
		String item3 = order.getNo() + "%";
		return basedao.getAllEntityByPage(hql, pageindex, pagesize, item1,
				item2, item3);
	}

	boolean checkOrder(Order order) {
		if (order == null || order.getNo() == null || order.getNo().isEmpty()) {
			return false;
		} else {
			return true;
		}
	}

}
